# coding:utf-8
import datetime
import sys
import time

import GuiBase
from PyQt5.QtWidgets import QApplication, QDialog
import logic
import threading


class MainDialog(QDialog):
    note = "BUG联系：3307129619"
    ui = None
    obj_logic = None
    threading_obj = None

    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.ui = GuiBase.Ui_Dialog()
        self.ui.setupUi(self)
        self.obj_logic = logic.logic(self.ui)
        self.ui.label_6.setText(self.note)
        self.ui.lineEdit_2.setText(self.obj_logic.url_get_account)
        self.ui.lineEdit_3.setText(self.obj_logic.url_get_red_packet)
        self.ui.lineEdit_4.setText(str(self.obj_logic.sleep_time))

    """
        获取账户
    """
    def getAccount(self):
        if self.ui.pushButton_2.text() == '停止':
            self.updateLog("请先停止运行再操作...")
            return False
        self.ui.textBrowser.setText('')
        self.obj_logic.account_id = None
        self.updateLog("正在查询账户...")
        res = self.obj_logic.getAccount(self.ui.lineEdit.text())
        if res['code'] == '0':
            id = ''
            server = ''
            level = ''
            lastTime = ''
            try:
                id = res['data']['id']
                server = res['data']['server']
                level = res['data']['level']
                lastTime = res['data']['lastTime']
                pass
            except BaseException as e:
                pass
            self.ui.textBrowser.setText(id)
            self.updateLog("查询成功，id：{},王国：{},等级：{},最近登录时间：{}".format(id, server, level, lastTime))
            pass
        else:
            self.updateLog("查询失败，{}".format(res['msg']))
            pass
        # 事件
        pass

    """
        坐标拾取器
    """
    def getWindowPoint(self):
        if self.ui.pushButton_2.text() == '停止':
            self.updateLog("请先停止运行再操作...")
            return False
        self.updateLog('正在拉起截图，请稍后.')
        self.threading_obj = threading.Thread(target=self.getWindowPointClick)
        self.threading_obj.start()
        pass

    """
        线程操作拉起截图获取坐标事件
    """
    def getWindowPointClick(self):
        self.obj_logic.getWindowPoint()
        temp_arr = [str(v) for v in self.obj_logic.window_point]
        temp_str = ' * '.join(temp_arr)
        self.ui.label_6.setText("{}|截图屏幕坐标：{}".format(self.note, temp_str))
        self.updateLog('选中屏幕坐标：{}'.format(temp_str))
        pass

    """
        更新设置
    """
    def updateSetting(self):
        self.obj_logic.url_get_account = self.ui.lineEdit_2.text()
        self.obj_logic.url_get_red_packet = self.ui.lineEdit_3.text()
        self.obj_logic.sleep_time = self.ui.lineEdit_4.text()
        self.updateLog('更新获取用户id地址：{}'.format(self.obj_logic.url_get_account))
        self.updateLog('更新领取地址：{}'.format(self.obj_logic.url_get_red_packet))
        self.updateLog('更新截屏等待时间：{}'.format(self.obj_logic.sleep_time))
        pass

    """
        开始运行
    """
    def runGrabWindow(self):
        if self.ui.pushButton_2.text() == '停止':
            # 停止
            self.obj_logic.stop = True
            self.ui.pushButton_2.setText("运行(RUN)")
            if self.threading_obj is not None: # 停止线程
                self.threading_obj.join()
                self.threading_obj = None
            return False
            pass
        else:
            self.ui.pushButton_2.setText("停止")
            pass
        if tuple(self.obj_logic.window_point) == (0, 0, 0, 0):
            self.updateLog('请先获取坐标')
            self.obj_logic.stop = True
            self.ui.pushButton_2.setText("运行(RUN)")
            return False
            pass
        if self.obj_logic.runGrabWindow_status:
            self.updateLog('请稍后，等待线程结束')
            self.obj_logic.stop = True
            return False
            pass
        self.obj_logic.stop = False
        self.ui.pushButton_2.setText("停止")
        self.threading_obj = threading.Thread(target=self.obj_logic.runGrabWindow)
        self.threading_obj.start()
        pass

    """
        更新日志
    """
    def updateLog(self, strval):
        times = datetime.datetime.now().strftime('%m-%d %H:%M:%S')
        self.ui.textBrowser_2.append("{}:{}".format(times, strval))
        self.ui.textBrowser_2.moveCursor(self.ui.textBrowser_2.textCursor().End)  # 文本框显示到底部
        pass


if __name__ == '__main__':
    myapp = QApplication(sys.argv)
    myDlg = MainDialog()
    myDlg.show()
    sys.exit(myapp.exec_())
